package com.hy.loong.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hy.loong.pojo.User;
import com.hy.loong.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "/user", description = "用户的相关操作")
@Controller
@RequestMapping("/user")
public class UserController {
	private static final Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String orderMatch(@Valid User user, BindingResult result, HttpSession session, Model model) {
		log.info("进入orderMatch：login");
		// 验证用户格式
		if (result.hasErrors()) {
			List<ObjectError> errorList = result.getAllErrors();
			for (ObjectError error : errorList) {
				System.out.println(error.getDefaultMessage());
			}
		}

		// userService.addUser(user);
		// System.out.println("user.getid:"+user.getId());
		User uu = userService.findUser(user);
		if (uu == null) {
			log.info("登录失败");
			return "index";
		}
		log.info("登录成功");
		session.setAttribute("user", user);
		model.addAttribute("username", user.getUsername());
		model.addAttribute("password", user.getPassword());
		return "login";
	}

	@ApiOperation(value = "登陆用户", notes = "登陆用户")
	@RequestMapping(value = "loginTest", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loginTest(@ApiParam(name ="user",value="登陆用户") @Valid User user, BindingResult result, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 验证用户格式
		if (result.hasErrors()) {
			List<ObjectError> errorList = result.getAllErrors();
			for (ObjectError error : errorList) {
				System.out.println(error.getDefaultMessage());
			}
		}

		User uu = userService.findUser(user);
		if (uu == null) {
			map.put("success", "false");
			map.put("view", "index");

		} else {
			session.setAttribute("user", user);
			map.put("success", "true");
			map.put("view", "login");
		}
		return map;

	}

	@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
	@RequestMapping(value = "getUserForid/{id}", method = RequestMethod.GET)
	@ResponseBody
	public User getUser(@PathVariable Integer id) {
		try {
			return userService.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
